﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Linq.Expressions;
using isct.escada.domain.core;

namespace isct.escada.infraestructure.data.configmodule.Core
{
    public class Repository<TEntity> :  IRepository<TEntity>  
        where TEntity:class
    {
        private IUnitOfWorkWithObjectSet m_UnitOw;
        private IObjectSet<TEntity> m_ObjectSet;

        public Repository(IUnitOfWorkWithObjectSet prmUnitOfWork)
        {
            m_UnitOw = prmUnitOfWork;
            m_ObjectSet = prmUnitOfWork.CreateSet<TEntity>();
        }

        #region Implementation of IRepository<TEntity>

        public IUnitOfWork UnitOfWork
        {
            get { return m_UnitOw; }
        }

        public void Add(TEntity item)
        {
            m_ObjectSet.AddObject(item);
        }

        public void Remove(TEntity item)
        {
            m_ObjectSet.DeleteObject(item);
        }

        public void Modify(TEntity item)
        {
            m_ObjectSet.Attach(item);
        }

        public IEnumerable<TEntity> GetAll()
        {
            return m_ObjectSet;
        }

        public IEnumerable<TEntity> GetPagedElements<TS>(int pageIndex, int pageCount, Expression<Func<TEntity, TS>> orderByExpression, bool ascending)
        {
            return null;
        }

        public IEnumerable<TEntity> GetFilteredElements(Expression<Func<TEntity, bool>> filter)
        {
            return m_ObjectSet.Where(filter);
        }

        #endregion

    
    }
}
